﻿using System.Collections.Generic;
using System.Windows;
using MyFloridaHouse.libSvDTO;
using MyFloridaHouse.libSvUtilities.Logging;

namespace MyFloridaHouse.libSvOpenDistricts.PDF
{
    public class PDFShapeNameManager
    {
        PDFdoc pdf;
        List<PDFShapeName> shapeNameList = new List<PDFShapeName>();
        public List<PDFShapeName> clippedShapeNameList = new List<PDFShapeName>();
        public List<PDFShapeName> clippedDistrictNameList = new List<PDFShapeName>();

        public PDFShapeNameManager(PDFdoc pdf)
        {
            this.pdf = pdf;
        }

        internal void RegisterDistrictLabel( int districtID, PDFString shapeName, PointDTO shapeCenterLatLon, BoundingBoxDTO shapeBoundingBoxLatLon, BoundingBoxDTO mapBoundingBoxLatLon, Rect mapInsetXYRect, double zoomLevel)
        {
            if (!(string.Empty == shapeName.text))
            {
                PDFShapeName newShapeName = new PDFShapeName(districtID, -1, shapeName, shapeCenterLatLon, shapeBoundingBoxLatLon, mapBoundingBoxLatLon, mapInsetXYRect, zoomLevel);
                clippedDistrictNameList.Add(newShapeName);
            }
        }

        public void RegisterLabel(int regionID, int layerID, PDFString shapeName, PointDTO shapeCenterLatLon, BoundingBoxDTO shapeBoundingBoxLatLon, BoundingBoxDTO mapBoundingBoxLatLon, Rect clipRect, double zoomLevel)
        {
            if (!(string.Empty == shapeName.text))
            {
                PDFShapeName newShapeName = new PDFShapeName(regionID, layerID, shapeName, shapeCenterLatLon, shapeBoundingBoxLatLon, mapBoundingBoxLatLon, clipRect, zoomLevel);
                shapeNameList.Add(newShapeName);
            }
        }

        public List<PDFClippedDistrictLabel> Draw()
        {
            // draw pre-defined shape labels
            foreach (var shapeName in shapeNameList)
            {
                shapeName.name.DrawCenteredX(shapeName.position.centerPoint, shapeName.mapInsetXYRect);
            }


            // draw district labels - should always be last thing drawn
            List<PDFClippedDistrictLabel> districtsMissingLabelsList = new List<PDFClippedDistrictLabel>();
            int clippedShapeID = 0;
            foreach (var districtName in clippedDistrictNameList)
            {
                if (!districtName.name.DrawInRectangle(districtName.position.centerPoint, districtName.mapInsetXYRect))
                {
                    // success drawing district labels, save it so we can remove from the list
                    PDFClippedDistrictLabel districtLabel = new PDFClippedDistrictLabel(clippedShapeID++, districtName);
                    districtsMissingLabelsList.Add(districtLabel);
                }

            }
            return districtsMissingLabelsList;

        }

    }
}
